/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;


import java.util.ArrayList;
import javax.servlet.http.HttpSession;
import model.*;
import model.entity.*;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author ToNghiep
 */
public class UrlPageAdmin extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UrlPageAdmin</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet UrlPageAdmin at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        String url = request.getServletPath();
        HttpSession se = request.getSession();
        CategoryImp cim = new CategoryImp();
        ProductImp pim = new ProductImp(); 
        AccountImp acim = new AccountImp();
        BillImp bim = new BillImp();
        if(url.equals("/admin"))
        {
                 
            if(se.getAttribute("admin")!=null)
            {
                request.setAttribute("page", "home.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
               
            }
            else
            {
                 RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/login.jsp");
                 dispatcher.forward(request, response);
            }
        
        }
        else if(url.equals("/logoutadmin"))
        {
            se.removeAttribute("admin");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/login.jsp");
            dispatcher.forward(request, response);
        }
        else if(url.equals("/admin/home"))
        {
            if(se.getAttribute("admin")!=null)
            {
                
                
                request.setAttribute("page", "home.jsp");
                RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                dis.forward(request, response);
            }
            else
            {
                 RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/login.jsp");
                 dispatcher.forward(request, response);
            }
            
            
        }
        
        else if(url.equals("/admin/account/credit"))
        {
            String username = request.getParameter("username");
            Account acccredit = acim.DetailAcc(username);
            request.setAttribute("acccredit", acccredit);
            request.setAttribute("page", "credit.jsp");
            RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
            dis.forward(request, response);
            
        }
        else if(url.equals("/admin/account/create"))
        {
            
                request.setAttribute("page", "newaccount.jsp");
                RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                dis.forward(request, response);
           
        }
        else if(url.equals("/admin/account/delete"))
        {
            if(se.getAttribute("admin")!=null)
            {
                String username = request.getParameter("username");
                
                if(acim.DeleteAccount(username, 2))
                {
                    request.setAttribute("message1", username + " has been deleted");
                    List<Account> listacc = acim.AllAccount();
                    request.setAttribute("listacc", listacc);
                    request.setAttribute("page", "account.jsp");
                    RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                    dis.forward(request, response);
                }
                
                
            }
            else
            {
                 RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/login.jsp");
                 dispatcher.forward(request, response);
            }
        }
        else if(url.equals("/admin/account/edit"))
        {
            
            String username = request.getParameter("username");
            request.setAttribute("username", username);
            request.setAttribute("page", "editaccount.jsp");
            RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
            dis.forward(request, response);
           
        }
        
        ////PRODUCT///////////////
        else if(url.equals("/admin/product"))
        {
            String categoryid = request.getParameter("categoryid");
            String branchid = request.getParameter("branchid");
            request.setAttribute("brandid", branchid);
            BranchProductImp bpim = new BranchProductImp();
            request.setAttribute("catid", categoryid);
            List<Product> lprod = new ArrayList<Product>();
            PrintWriter out = response.getWriter();
            if(categoryid == null && branchid == null)
            {
                lprod = pim.Products();
            }
            else if(branchid != null)
            {
                List<Integer> listproductid = bpim.GetProductId(Integer.valueOf(branchid));
                for(Integer i : listproductid)
                {
                    lprod.add(pim.DetailProduct(i));
                }
            }
            else if(categoryid != null)
            {
                lprod = pim.ProductsWithCategory(Integer.valueOf(categoryid));
            }
            
            request.setAttribute("listproduct", lprod);
            request.setAttribute("page", "product.jsp");
            RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
            dis.forward(request, response);

        }
        else if(url.equals("/admin/product/delete"))
        {
            int productid = Integer.valueOf(request.getParameter("productid"));
            String name = pim.DetailProduct(productid).getProductname();
            if(pim.DeleteProduct(productid))
            {
                    List<Product> lprod = pim.Products();    
                    request.setAttribute("messagedelete", name  + " has been deleted");
                    request.setAttribute("listproduct", lprod);
                    request.setAttribute("page", "product.jsp");
                    RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                    dis.forward(request, response);
                }
                else
                {
                    List<Product> lprod = pim.Products(); 
                    request.setAttribute("messagedelete", name  + " could not delete");
                    request.setAttribute("listproduct", lprod);
                    request.setAttribute("page", "product.jsp");
                    RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                    dis.forward(request, response);
                }
        }
         else if(url.equals("/admin/product/edit"))
        {
                
                int productid = Integer.valueOf(request.getParameter("productid"));
                
                Product prod = new Product();
                prod = pim.DetailProduct(productid);
                request.setAttribute("editproduct", prod);
                request.setAttribute("page", "editproduct.jsp");
                RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                dis.forward(request, response);
            
        }
        else if(url.equals("/admin/product/create"))
        {
                List<Category> lcat = cim.ChildCategory();
                request.setAttribute("lcat", lcat);
                request.setAttribute("page", "createproduct.jsp");
                RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                dis.forward(request, response);
        }
        
        //CATEGORY///////////
        else if(url.equals("/admin/category"))
        {
                
                request.setAttribute("page", "category.jsp");
                RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                dis.forward(request, response);
           
        }
        else if(url.equals("/admin/category/parent"))
        {
                
                request.setAttribute("page", "category.jsp");
                RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                dis.forward(request, response);
           
        }
        else if(url.equals("/admin/category/child"))
        {
  
             request.setAttribute("page", "child.jsp");
             RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
             dis.forward(request, response);
           
        }
        else if(url.equals("/admin/category/delete"))
        {
            if(se.getAttribute("admin")!=null)
            {
               int categoryid = Integer.valueOf(request.getParameter("categoryid"));
                String name = cim.DetailCategory(categoryid).getCategoryname();
                if(cim.DeleteCategory(categoryid))
                {
                    request.setAttribute("messcategory", name  + " has been deleted");
                    request.setAttribute("page", "child.jsp");
                    RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                    dis.forward(request, response);
                }
                else
                {
                    request.setAttribute("messcategory", name  + " could not delete");
                    request.setAttribute("page", "child.jsp");
                    RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                    dis.forward(request, response);
                }
                
            }
            else
            {
                 RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/login.jsp");
                 dispatcher.forward(request, response);
            }
        }
        else if(url.equals("/admin/category/edit"))
        {
                
                int categoryid = Integer.valueOf(request.getParameter("categoryid"));
                Category cat = cim.DetailCategory(categoryid);
                request.setAttribute("editcat", cat);
                request.setAttribute("page", "editcategory.jsp");
                RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                dis.forward(request, response);
            
        }
        else if(url.equals("/admin/category/create"))
        {
                List<Category> lcat = cim.ParentCategory();
                lcat.add(new Category(0, "Root", -1));
                request.setAttribute("lcat", lcat);
                request.setAttribute("page", "createcategory.jsp");
                RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                dis.forward(request, response);
        }
        
        else if(url.equals("/admin/branch"))
        {
            BranchImp brim = new BranchImp();
            List<Branch> listbranch = brim.Branchs();
            request.setAttribute("listbranch", listbranch);
            request.setAttribute("page", "branch.jsp");
            RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
            dis.forward(request, response);
        }
        ////BILL
        else if(url.equals("/admin/bill"))
        {
                List<Bill> lstbill = bim.AllBill();
                request.setAttribute("listbill", lstbill);
                request.setAttribute("page", "bill.jsp");
                RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                dis.forward(request, response);
        }
        else if(url.equals("/admin/bill/detail"))
        {
            String billid = request.getParameter("billid");
            Bill bill = bim.BillDetail(billid);
            request.setAttribute("bill", bill);
            request.setAttribute("page", "billdetail.jsp");
            RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
            dis.forward(request, response);
        }
        else if(url.equals("/admin/branchproduct/add"))
        {
            int productid = Integer.valueOf(request.getParameter("productid"));
            BranchImp brim = new BranchImp();
            List<Branch> listbranch = brim.Branchs();
            request.setAttribute("listbranch", listbranch);
            request.setAttribute("productid", productid);
            request.setAttribute("page", "addtobranch.jsp");
            RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
            dis.forward(request, response);
//            PrintWriter out = response.getWriter();
//            out.print(productid);
        }
        else if(url.equals("/admin/account"))
        {
            
            List<Account> listacc = acim.AllAccount(); 
            request.setAttribute("listacc", listacc);
            request.setAttribute("page", "account.jsp");
            RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
            dis.forward(request, response);
        }
        
        
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        String url = request.getServletPath();
        HttpSession se = request.getSession();
        CategoryImp cim = new CategoryImp();
        AccountImp acim = new AccountImp();
        ProductImp pim = new ProductImp();
        BillImp bim = new BillImp();
        if(url.equals("/admin"))
        {
                 
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            
            Account acc = acim.DetailAcc(username);
            if(acim.Login(username, password, 1))
            {

                se.setAttribute("admin", acc);
                request.setAttribute("page", "home.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);        
            }
            else
            {
                request.setAttribute("message", "Username or password incorrect");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/login.jsp");
                dispatcher.forward(request, response);
            }
        }
        
        ////////////////ACCCOUNT////////////////////////
        else if(url.equals("/admin/account/create"))
        {
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                String firstname = request.getParameter("firstname");
                String lastname = request.getParameter("lastname");
                String country = request.getParameter("country");
                String city = request.getParameter("city");
                String state_province = request.getParameter("state_province");
                String street = request.getParameter("street");
                String homephone = request.getParameter("homephone");
                String mobile = request.getParameter("mobile");
                String typecard = request.getParameter("typecard");
                String cardnumber = request.getParameter("cardnumber");
                String cardexpiry = request.getParameter("cardexpiry");
                int role = Integer.valueOf(request.getParameter("role"));
                String email = request.getParameter("email");
               
                    if(acim.CreateAccountadm(username, password, firstname, lastname, country, city, state_province, street, homephone, mobile, typecard, cardnumber, cardexpiry, role, email, role))
                    {
                        request.setAttribute("messagecreate", username + " has been added to account");
                         List<Account> listacc = acim.AllAccount();
                        request.setAttribute("listacc", listacc);
                        request.setAttribute("page", "account.jsp");
                        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                        dispatcher.forward(request, response);
                    }
                    else
                    {
                        request.setAttribute("page", "newaccount.jsp");
                        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                        dispatcher.forward(request, response);
                        
                        
                    }
   
        
        }
        else if(url.equals("/admin/account/edit"))
        {
            
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                String firstname = request.getParameter("firstname");
                String lastname = request.getParameter("lastname");
                String country = request.getParameter("country");
                String city = request.getParameter("city");
                String state_province = request.getParameter("state_province");
                String street = request.getParameter("street");
                String homephone = request.getParameter("homephone");
                String mobile = request.getParameter("mobile");
                String typecard = request.getParameter("typecard");
                String cardnumber = request.getParameter("cardnumber");
                String cardexpiry = request.getParameter("cardexpiry");
                
                String email = request.getParameter("email");
             
                    if(acim.EditAccount(username, password, firstname, lastname, country, city, state_province, street, homephone, mobile, typecard, cardnumber, cardexpiry, email))
                    {
                        request.setAttribute("messageedit", username+" has been edited");
                        List<Account> listacc = acim.AllAccount();
                        request.setAttribute("listacc", listacc);
                        request.setAttribute("page", "account.jsp");
                        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                        dispatcher.forward(request, response);
                    }
                    else
                    {
                        request.setAttribute("messageedit", username+" coulded not edit");
                        List<Account> listacc = acim.AllAccount();
                        request.setAttribute("listacc", listacc);
                        request.setAttribute("page", "account.jsp");
                        RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                        dispatcher.forward(request, response);
                    } 
        }
              ////////////////ACCCOUNT////////////////////////
       ///////////CATEGORY////////
        else if(url.equals("/admin/category/edit"))
        {
         
            int categoryid = Integer.valueOf(request.getParameter("categoryid"));
            String old = request.getParameter("oldcategoryname");
            String cagtegoryname = request.getParameter("categoryname");
            int parentid = Integer.valueOf(request.getParameter("parentid"));
            
            if(cim.EditCategory(categoryid, cagtegoryname, parentid))
            {
                request.setAttribute("messageeditcat", old + " edited to " + cagtegoryname );
                request.setAttribute("page", "category.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
            }
            else
            {
                request.setAttribute("messageeditcat", old + " could not edit " );
                request.setAttribute("page", "editcategory.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
            }
        }
        else if(url.equals("/admin/category/create"))
        {
           String categoryname = request.getParameter("categoryname");
           int parentid = Integer.valueOf(request.getParameter("parentid"));
           if(cim.CreateCategory(categoryname, parentid))
           {
               request.setAttribute("messagecreate", categoryname + " has been add to category"); 
               request.setAttribute("page", "category.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
           }
           else
           {
               request.setAttribute("messagecreate", categoryname + " could not create"); 
               request.setAttribute("page", "category.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
           }
        }
        
         ///////////CATEGORY////////
        //PRODUCT////////
        else if(url.equals("/admin/product/create"))
        {      
          
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        System.out.println("request: " + request);
        if (!isMultipart) {
            request.setAttribute("messagecreate", "Please try again");
            request.setAttribute("page", "createproduct.jsp");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
            dispatcher.forward(request, response);
        } else {

            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            Hashtable ht = new Hashtable();
            String productname = "";
            String picture = "";
            int categoryid = 0;
            int quantity = 0;
            double totalamount = 0;
            String prodtype = "";
            double unitcost = 0;
            String productdetail = "";
            try {
                List items = upload.parseRequest(request);
                Iterator iter = items.iterator();
                
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    if (item.isFormField())
                    {
                       
                       String pname = item.getFieldName();  
                       if(pname.equals("categoryid"))
                       {
                        categoryid = Integer.valueOf(item.getString());
                      
                       }
                       else if(pname.equals("productname"))
                       {
                            productname = item.getString();  
                       }
                       else if(pname.equals("quantity"))
                       {
                           quantity = Integer.valueOf(item.getString());
                       }
                       else if(pname.equals("unitcost"))
                       {
                           unitcost = Double.valueOf(item.getString());
                       }
                       else if(pname.equals("prodtype"))
                       {
                           prodtype = item.getString();
                       }
                       else if(pname.equals("productdetail"))
                       {
                           productdetail = item.getString();
                       }
                       
                    } else {

                       
                        String[] filetype = item.getContentType().split("/");
                        if (filetype[0].equals("image")) 
                        {
                            String uud = UUID.randomUUID().toString();
                            String patch = getServletContext().getRealPath("/");
                            File savefile = new File(patch + "images/product/" + uud+item.getName());
                            item.write(savefile);
                            picture = uud+item.getName();
                            
                        } 
                        else
                        {
                            request.setAttribute("messagecreate", "Please choose image");
                            request.setAttribute("page", "createproduct.jsp");
                            RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                            dispatcher.forward(request, response);
                        }
                        
                    }
                   
                    
                }

            } catch (Exception e) {
                request.setAttribute("messagecreate", "Create product faild");
                request.setAttribute("page", "product.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
            }
            
          //code here
            if(prodtype.equals("Discount"))
            {
                totalamount = unitcost - unitcost/10;
            }
            else if(prodtype.equals("Special"))
            {
                 totalamount = unitcost + unitcost/10;
            }
            else if(prodtype.equals("Normal"))
            {
                totalamount = unitcost;
            }
            if(pim.CreateProduct(categoryid, quantity, productname, unitcost, totalamount, picture, productdetail, prodtype))
            {
                List<Product> lprod = pim.Products();
                request.setAttribute("listproduct", lprod);
                request.setAttribute("messagecreate", "Create product success");
                request.setAttribute("page", "product.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
            }
            else
            {
                request.setAttribute("messagecreate", "Create product faild");
                List<Product> lprod = pim.Products();
                request.setAttribute("listproduct", lprod);
                request.setAttribute("page", "product.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
            }
        }


        }
        else if(url.equals("/admin/product/edit"))
        {
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        System.out.println("request: " + request);
        if (!isMultipart) {
            request.setAttribute("messagecreate", "Please try again");
            request.setAttribute("page", "createproduct.jsp");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
            dispatcher.forward(request, response);
        } else {

            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            Hashtable ht = new Hashtable();
            int productid = 0;
            String productname = "";
            String picture = "";
            int categoryid = 0;
            int quantity = 0;
            String prodtype = "";
            double totalamount = 0;
            double unitcost = 0;
            String productdetail = "";
            try {
                List items = upload.parseRequest(request);
                Iterator iter = items.iterator();
                
                while (iter.hasNext()) {
                    FileItem item = (FileItem) iter.next();
                    if (item.isFormField())
                    {
                       
                       String pname = item.getFieldName();  
                       
                       if(pname.equals("categoryid"))
                       {
                        categoryid = Integer.valueOf(item.getString());
                      
                       }
                       else if(pname.equals("productid"))
                       {
                           productid = Integer.valueOf(item.getString());
                       }   
                       else if(pname.equals("productname"))
                       {
                            productname = item.getString();  
                       }
                       else if(pname.equals("quantity"))
                       {
                           quantity = Integer.valueOf(item.getString());
                       }
                       else if(pname.equals("unitcost"))
                       {
                           unitcost = Double.valueOf(item.getString());
                       }
                       else if(pname.equals("prodtype"))
                       {
                           prodtype = item.getString();
                       }
                       else if(pname.equals("productdetail"))
                       {
                           productdetail = item.getString();
                       }
                       else if(pname.equals("picture"))
                       {
                           picture = item.getString();
                       }
                       
                    } else {

                       
                        String[] filetype = item.getContentType().split("/");
                        if (filetype[0].equals("image")) 
                        {
                            String uud = UUID.randomUUID().toString();
                            String patch = getServletContext().getRealPath("/");
                            File savefile = new File(patch + "images/product/" + uud+item.getName());
                            item.write(savefile);
                            picture = uud+item.getName();
                            
                        } 

                    }
                   
                    
                }

            } catch (Exception e) {
                request.setAttribute("messageedit", "Edit product faild");
                request.setAttribute("page", "editproduct.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
            }
            if(prodtype.equals("Discount"))
            {
                totalamount = unitcost - unitcost/10;
            }
            else if(prodtype.equals("Special"))
            {
                 totalamount = unitcost + unitcost/10;
            }
            else if(prodtype.equals("Normal"))
            {
                totalamount = unitcost;
            }
            

             if(pim.EditProduct(productid, categoryid, quantity, productname, unitcost, totalamount, picture, productdetail, prodtype))
            {
                List<Product> lprod = pim.Products();
                request.setAttribute("listproduct", lprod);
                request.setAttribute("messageedit", "Edit product success");
                request.setAttribute("page", "product.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
            }
            else
            {
                List<Product> lprod = pim.Products();
                request.setAttribute("listproduct", lprod);
                request.setAttribute("messageedit", "Edit product faild");
                request.setAttribute("page", "product.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);
            }
        }
        
        }
        else if(url.equals("/admin/bill/detail"))
        {
            String billid = request.getParameter("billid");
            String Status = request.getParameter("status");
            if(bim.EditBill(billid, Status))
            {
                List<Bill> lstbill = bim.AllBill();
                request.setAttribute("listbill", lstbill);
                request.setAttribute("messageedit",billid + "is edit");
                request.setAttribute("page", "bill.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);   
            }else
                {
                List<Bill> lstbill = bim.AllBill();
                request.setAttribute("listbill", lstbill);
                    request.setAttribute("messageedit",billid + "is not edit");
                request.setAttribute("page", "bill.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response);   
            }
        }
        else if(url.equals("/admin/account/credit"))
        {
            String username = request.getParameter("username");
            Account acc = acim.DetailAcc(username);
            double creditadd = Double.valueOf(request.getParameter("credit"));
            double credit = creditadd + acc.getCredit();
            if(acim.Credit(username, credit))
            {
                request.setAttribute("credit",username + " added " + creditadd + " to account");
                List<Account> listacc = acim.AllAccount();
                request.setAttribute("listacc", listacc);
                 request.setAttribute("page", "account.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response); 
            }
        }
        else if(url.equals("/admin/branchproduct/add"))
        {
            try {
             String s = request.getParameter("sanphamid");
               System.out.println(s);
                int productid = Integer.valueOf(request.getParameter("sanphamid"));
            int branchid = Integer.valueOf(request.getParameter("branchid"));
          
            
            BranchProductImp bpim = new BranchProductImp();

            if(bpim.AddProduct(productid, branchid))
            {
                request.setAttribute("listproduct", pim.Products());
                request.setAttribute("messageaddtobranch", "success");
                List<Account> listacc = acim.AllAccount();
                request.setAttribute("listacc", listacc);
                request.setAttribute("page", "product.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response); 
            }
            else
            {
                request.setAttribute("messageaddtobranch", "faild");
                request.setAttribute("page", "addtobranch.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/views/admin/index.jsp");
                dispatcher.forward(request, response); 
            }
                
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            
        }
        
        else if(url.equals("/admin/searchuser"))
        {
                
            String username = request.getParameter("usersearch");
            PrintWriter out = response.getWriter();
            out.print(username);
                List<Account> listacc = new ArrayList<Account>();
                listacc = acim.AllAccount();                  
                request.setAttribute("listacc", listacc);
                request.setAttribute("page", "account.jsp");
                RequestDispatcher dis = request.getRequestDispatcher("/views/admin/index.jsp");
                dis.forward(request, response);

        }
 
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

